﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeDynamiksEngine;
using MESCodeFile;
using LiveStateSystem;

namespace mes_tublay
{
    partial class frmUserManager
    {

        //The following code contains all handles relating to
        //save stating. Please follow the pattern accordingly.
        //Please note that the state.Run() method is being called somewhere on
        //the GUI code file.
        SaveState state;
        void InitializeSaveState()
        {
            //Instantiate a savestate(save state takes place every 10 sec.)
            state = new SaveState(CurrentUser.USERIDNUMBER + this.Name, Common.LOCPATH, 10000);
            
            //Code for fetching states to save.
            state.StateFetch += new EventHandler(delegate
            {
                //Saving of states takes place here.
                proc.SafeCall(this, delegate
                {
                    state.Clear();
                    //Variable states
                    state.Add("frmUserManager.hasImage", hasImage.ToString());
                    state.Add("frmUserManager.isAddNew", isAddNew.ToString());
                    state.Add("frmUserManager.isEditorMode", isEditorMode.ToString());
                    state.Add("frmUserManager.userToEdit", userToEdit);
                    state.Add("frmUserManager._uid", _uid);
                    state.Add("frmUserManager._pwd", _pwd);

                    //Control states
                    state.Add("frmUserManager.txtUID.Text", txtUID.Text);
                    state.Add("frmUserManager.txtPWD.Text", txtPWD.Text);
                    state.Add("frmUserManager.txtName.Text", txtName.Text);
                    state.Add("frmUserManager.txtAddress.Text", txtAddress.Text);
                    state.Add("frmUserManager.txtContactNum.Text", txtContactNum.Text);
                    state.Add("frmUserManager.grpUserDetails.Enabled", grpUserDetails.Enabled);
                    state.Add("frmUserManager.btnAddNew.Enabled", btnAddNew.Enabled);
                    state.Add("frmUserManager.lstUsers.Enabled", lstUsers.Enabled);
                    state.Add("frmUserManager.btnRemPriv.Enabled", btnRemPriv.Enabled);

                    //Save stating a combobox.
                    state.Add("frmUserManager.txtPrivilege.Count", txtPrivilege.Items.Count);
                    state.Add("frmUserManager.txtPrivilege.Text", txtPrivilege.Text);
                    for (int i = 0; i < txtPrivilege.Items.Count; i++)
                    {
                        state.Add("frmUserManager.txtPrivilege." + i, txtPrivilege.Items[i]);
                    }
                    //save stating a listbox
                    state.Add("frmUserManager.lstPrivilege.Count", lstPrivilege.Items.Count);
                    state.Add("frmUserManager.lstPrivilege.SelectedIndex", lstPrivilege.SelectedIndex);
                    for (int i = 0; i < lstPrivilege.Items.Count; i++)
                    {
                        state.Add("frmUserManager.lstPrivilege." + i, lstPrivilege.Items[i]);
                    }
                    //Call save method at the end.
                    state.Save();
                });
            });
            //Code for loading save states to variables and controls.
            state.StateChanged += new EventHandler(delegate
            {
                if (state.HasError)
                {
                    Confirm.Error("SaveState", state.Message);
                    return;
                }
                proc.SafeCall(this, delegate
                {
                    //Loading variables   
                    hasImage = bool.Parse(state.State.Read("frmUserManager.hasImage").ToString());
                    isAddNew = bool.Parse(state.State.Read("frmUserManager.isAddNew").ToString());
                    isEditorMode = bool.Parse(state.State.Read("frmUserManager.isEditorMode").ToString());
                    userToEdit = int.Parse(state.State.Read("frmUserManager.userToEdit").ToString());
                    _uid = state.State.Read("frmUserManager._uid").ToString();
                    _pwd = state.State.Read("frmUserManager._pwd").ToString();

                    //Do something with the loaded variable
                    if (hasImage)
                    {
                        try
                        {
                            using (System.IO.Stream fs = System.IO.File.Open(Common.LOCPATH + CurrentUser.USERIDNUMBER + Const.USERMANAGERIMG, System.IO.FileMode.Open))
                            {
                                imgUser.Image = new Bitmap(fs);
                                fs.Close();
                            }
                        }
                        catch (Exception)
                        {
                            Confirm.Error("User Image", "There was a problem loading your user image.");
                            imgUser.Image = new Bitmap(480, 480);
                        }
                    }
                   
                    //Loading control values
                    txtUID.Text = state.State.Read("frmUserManager.txtUID.Text").ToString();
                    txtPWD.Text = state.State.Read("frmUserManager.txtPWD.Text").ToString();
                    txtName.Text = state.State.Read("frmUserManager.txtName.Text").ToString();
                    txtAddress.Text = state.State.Read("frmUserManager.txtAddress.Text").ToString();
                    txtContactNum.Text = state.State.Read("frmUserManager.txtContactNum.Text").ToString();
                    grpUserDetails.Enabled = bool.Parse(state.State.Read("frmUserManager.grpUserDetails.Enabled").ToString());
                    btnAddNew.Enabled = bool.Parse(state.State.Read("frmUserManager.btnAddNew.Enabled").ToString());
                    lstUsers.Enabled = bool.Parse(state.State.Read("frmUserManager.lstUsers.Enabled").ToString());
                    btnRemPriv.Enabled = bool.Parse(state.State.Read("frmUserManager.btnRemPriv.Enabled").ToString());

                    //Loading values to a combobox.
                    int Cnt = int.Parse(state.State.Read("frmUserManager.txtPrivilege.Count").ToString());
                    txtPrivilege.Items.Clear();
                    for (int i = 0; i < Cnt; i++)
                    {
                        txtPrivilege.Items.Add(state.State.Read("frmUserManager.txtPrivilege." + i));
                    }
                    txtPrivilege.Text = state.State.Read("frmUserManager.txtPrivilege.Text").ToString();

                    //Loading values to a list box.
                    Cnt = int.Parse(state.State.Read("frmUserManager.lstPrivilege.Count").ToString());
                    for (int i = 0; i < Cnt; i++)
                    {
                        lstPrivilege.Items.Add(state.State.Read("frmUserManager.lstPrivilege." + i));
                    }
                    lstPrivilege.SelectedIndex = int.Parse(state.State.Read("frmUserManager.lstPrivilege.SelectedIndex").ToString());
                    if(!state.Running) state.Run();
                });
            });
            //When the user clicks the close button, flush the save states
            btnClose.Click += new EventHandler(delegate
            {
                state.Flush();
            });
        }//SaveState Operation
    }
    //Mga sir, the purpose of this code file is to organize the calls
    //related to save states. All save state handles goes here.
    //Please call state.Run() from your GUI code. You have to decide when the 
    //save state starts fetching data and what data to save. Please refer
    //to frmUserManager class above for reference on how to fetch and load data.
    public partial class frmApplicationBusinessPermit
    {
        SaveState state;
        void InitializeSaveState()
        {
            //Instantiate a savestate(save state takes place every  10 sec.)
            state = new SaveState(CurrentUser.USERIDNUMBER + this.Name, Common.LOCPATH, 10000);
            //Code for fetching states to save.
            state.StateFetch += new EventHandler(delegate
            {
                //Saving of states takes place here.
                proc.SafeCall(this, delegate
                {
                    state.Clear();
                    string f = "frmApplicationBusinessPermit.";
                    //Variable states
                    state.Add(f + "businessReferenceNumber", businessReferenceNumber);
                    
                    state.Add(f + "businessOwner.fID", businessOwner.fID);
                    state.Add(f + "businessOwner.fStamp", businessOwner.fStamp);
                    state.Add(f + "businessProfile.fID", businessProfile.fID);
                    state.Add(f + "businessProfile.fStamp", businessProfile.fStamp);
                    state.Add(f + "businessInformation.fID", businessInformation.fID);
                    state.Add(f + "businessInformation.fStamp", businessInformation.fStamp);
                    state.Add(f + "referenceNumberInfo.fID", referenceNumberInfo.fID);
                    state.Add(f + "referenceNumberInfor.fStamp", referenceNumberInfo.fStamp);
                    state.Add(f + "applicationNumberInfo.fID", applicationNumberInfo.fID);
                    state.Add(f + "applicationNumberInfo.fStamp", applicationNumberInfo.fStamp);

                    state.Add(f + "isExistingDate", isExistingDate);
                    state.Add(f + "isExisitngOwnerProfile", isExisitngOwnerProfile);
                    state.Add(f + "isOnError", isOnError);
                    state.Add(f + "businessType", businessType);

                    state.Add(f + "currentRefNum", currentRefNum);
                    state.Add(f + "myAction", myAction);
                    state.Add(f + "myReferenceNumber", myReferenceNumber);
                    state.Add(f + "myBusinessReferenceNumber", myBusinessReferenceNumber);
                    state.Add(f + "myPreviousApplicationNumber", myPreviousApplicationNumber);

                    //Control states (Basic Information Tab)
                    //Top and Left
                    state.Add(f + "txtDateOfApplication.Value", txtDateOfApplication.Value.ToString());
                    state.Add(f + "txtKindOfOrganization.Text", txtKindOfOrganization.Text);
                    state.Add(f + "txtApplicationNumber.Text", txtApplicationNumber.Text);
                    state.Add(f + "txtReferenceNumber.Text", txtReferenceNumber.Text);

                    state.Add(f + "single", single);
                    state.Add(f + "partnership", partnership);
                    state.Add(f + "corporation", corporation);

                    state.Add(f + "txtLastName.Text", txtLastName.Text);
                    state.Add(f + "txtFistName.Text", txtFirstName.Text);
                    state.Add(f + "txtMiddleName.Text", txtMiddleName.Text);

                    state.Add(f + "cmbGender.Text", cmbGender.Text);
                    state.Add(f + "cmbCivilStatus.Text", cmbCivilStatus.Text);
                    state.Add(f + "txtCitizenship.Text", txtCitizenship.Text);
                    //Right
                    state.Add(f + "txtCompanyName.Text", txtCompanyName.Text);
                    state.Add(f + "txtKindOfBusiness.Text", txtKindOfBusiness.Text);
                    state.Add(f + "txtBusinessName.Text", txtBusinessName.Text);
                    state.Add(f + "txtTradeNameFranchise.Text", txtTradeNameFranchise.Text);
                    state.Add(f + "chkYes.Checked", chkYes.Checked.ToString());
                    state.Add(f+ "txtSpecify.Text", txtSpecify.Text);

                    //For Corporation/Association Tab
                    state.Add(f + "txtOHouseNumber.Text", txtOHouseNumber.Text);
                    state.Add(f + "txtOBuildingName.Text", txtOBuildingName.Text);
                    state.Add(f + "txtOUnitNumber.Text", txtOUnitNumber.Text);
                    state.Add(f + "txtOSitio.Text", txtOSitio.Text);
                    state.Add(f + "txtOBarangay.Text", txtOBarangay.Text);
                    state.Add(f + "txtOSubdivision.Text", txtOSubdivision.Text);
                    state.Add(f + "txtOMunicipality.Text", txtOMunicipality.Text);
                    state.Add(f + "txtOProvince.Text", txtOProvince.Text);
                    state.Add(f + "txtOContactNumber.Text", txtOContactNumber.Text);
                    state.Add(f + "TxtOEmailAddress.Text", txtOEmailAddress.Text);

                    state.Add(f + "txtBHouseNumber.Text", txtBHouseNumber.Text);
                    state.Add(f + "txtBBuildingName.Text", txtBBuildingName.Text);
                    state.Add(f + "txtBUnitNumber.Text", txtBUnitNumber.Text);
                    state.Add(f + "txtBSitio.Text", txtBSitio.Text);
                    state.Add(f + "txtBBarangay.Text", txtBBarangay.Text);
                    state.Add(f + "txtBSubdivision.Text", txtBSubdivision.Text);
                    state.Add(f + "txtBMunicipality.Text", txtBMunicipality.Text);
                    state.Add(f + "txtBProvince.Text", txtBProvince.Text);
                    state.Add(f + "txtBContactNumber.Text", txtBContactNumber.Text);
                    state.Add(f + "TxtBEmailAddress.Text", txtBEmailAddress.Text);

                    state.Add(f + "txtPIN.Text", txtPIN.Text);
                    state.Add(f + "txtBusinessArea.Text", txtBusinessArea.Text);
                    state.Add(f + "txtNEmployeesEstablishment.Value", txtNEmployeesEstablishment.Value);
                    state.Add(f + "txtNEmployeesTublay.Value", txtNEmployeesTublay.Value);
                    state.Add(f + "chkSame.Checked", chkSame.Checked.ToString());

                    //Additional Information Tab
                    state.Add(f + "txtLLastName.Text", txtLLastName.Text);
                    state.Add(f + "txtLFirstName.Text", txtLFirstName.Text);
                    state.Add(f + "txtLMiddleName.Text", txtLMiddleName.Text);
                    state.Add(f + "txtLAddress.Text", txtLAddress.Text);
                    state.Add(f + "txtLContactNumber.Text", txtLContactNumber.Text);
                    state.Add(f + "txtLEmailAddress.Text", txtLEmailAddress.Text);
                    state.Add(f + "txtMonthlyRental.Value", txtMonthlyRental.Value);

                    //Sample
                    //state.Add("frmUserManager.txtUID.Text", txtUID.Text);
                    
                    //Call save method at the end.
                    state.Save();
                });
            });
            //Code for loading save states to variables and controls.
            state.StateChanged += new EventHandler(delegate
            {
                if (state.HasError)
                {
                    Confirm.Error("SaveState", state.Message);
                    return;
                }
                proc.SafeCall(this, delegate
                {
                    //Loading variables   
                    //Sample
                    //hasImage = bool.Parse(state.State.Read("frmUserManager.hasImage").ToString());
                    
                    //Loading control values
                    //Sample
                    //txtUID.Text = state.State.Read("frmUserManager.txtUID.Text").ToString();
                    if (!state.Running) state.Run();
                });
            });
            //When the user clicks the close button, flush the save states
            btnClose.Click += new EventHandler(delegate
            {
                state.Flush();
            });
        }
    }
}